package gpl.scotlandyard.beans.adapters;

import gpl.scotlandyard.beans.basics.Board;
import gpl.scotlandyard.beans.basics.Player;
import gpl.scotlandyard.utils.I18n;

import javax.swing.table.AbstractTableModel;

/**
 * Represents the tickets board for all player.
 * @author Norbert Martin
 */
public class TicketsBoardTM extends AbstractTableModel {

  private static final long serialVersionUID = -7228326898276067329L;
  private static final int NUM_COLUMNS = 6;
  private final Board board;

  /**
   * Creates a tickets board for this game board.
   * @param model
   */
  public TicketsBoardTM(Board model) {
    board = model;
    for (Player player : model.getPlayersQueue()) {
      player.addListener(new Player.ListenerImpl() {
        @Override
        public void playerTicketsUpdated(Player player) {
          fireTableDataChanged();
        }
      });
    }
  }

  @Override
  public int getRowCount() {
    return board.getDetectives().size() + 1;
  }

  @Override
  public int getColumnCount() {
    return NUM_COLUMNS;
  }

  @Override
  public Object getValueAt(int rowIndex, int columnIndex) {
    Player p;
    if (rowIndex == board.getDetectives().size()) {
      p = board.getFugitive();
    } else {
      p = board.getDetectives().get(rowIndex);
    }
    if (columnIndex == 0) {
      return p.getName() + (p.isFugitive() ? "[F]" : "");
    }
    if (columnIndex == 1) {
      return p.getTaxi();
    }
    if (columnIndex == 2) {
      return p.getBus();
    }
    if (columnIndex == 3) {
      return p.getUg();
    }
    if (columnIndex == 4) {
      return p.getHide();
    }
    if (columnIndex == 5) {
      return p.getTwice();
    }
    throw new IndexOutOfBoundsException();
  }

  @Override
  public String getColumnName(int column) {
    if (column == 0) {
      return I18n.get("PLAYER");
    }
    if (column == 1) {
      return I18n.get("TAXI");
    }
    if (column == 2) {
      return I18n.get("BUS");
    }
    if (column == 3) {
      return I18n.get("UG");
    }
    if (column == 4) {
      return I18n.get("HIDE");
    }
    if (column == 5) {
      return I18n.get("TWICE");
    }
    throw new IndexOutOfBoundsException();
  }

}
